package zust.javaee.service.Impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zust.javaee.dao.hibernate.BaseDao;
import zust.javaee.dto.activity.ActivityDto;
import zust.javaee.entity.Tactivity;
import zust.javaee.service.ActivityService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liy on 2017/1/9.
 */
@Service
@Transactional
public class ActivityServiceImpl extends BaseServiceImpl<Tactivity> implements ActivityService {

    @Autowired
    private BaseDao<Tactivity> activityDao;



    @Override
    public ActivityDto transToDto(Tactivity tactivity) {
        ActivityDto activityDto = new ActivityDto();
        BeanUtils.copyProperties(tactivity, activityDto);
        return activityDto;
    }

    @Override
    public List<ActivityDto> transToDtos(List<Tactivity> tactivities) {
        List<ActivityDto> activityDtos = new ArrayList<>();
        for (Tactivity tactivity : tactivities)
            activityDtos.add(transToDto(tactivity));
        return activityDtos;
    }

    @Override
    public List<ActivityDto> transToDtos(Integer playerId, List<Tactivity> tactivities) {
        List<ActivityDto> activityDtos = new ArrayList<>();
        for (Tactivity tactivity : tactivities){
            ActivityDto act = transToDto(tactivity);

            activityDtos.add(act);
        }

        return activityDtos;
    }

    @Override
    public Tactivity getActivityById(Integer id) {
        String hql = "from Tactivity a where a.id = " + id;
        return activityDao.get(hql);
    }

    @Override
    public List<Tactivity> getActivityByCorporId(Integer corporId) {
        String hql = "from Tactivity a where a.tcorporation.id = " + corporId;
        return activityDao.find(hql);
    }

    @Override
    public List<Tactivity> getActivityByCorporId(Integer corporId, Integer num) {
        String hql = "from Tactivity a where a.tcorporation.id = " + corporId;
        return activityDao.find(hql, 1, num);
    }
}
